package com.coocaa.ops.admin.v1.tab.validator;

import com.coocaa.ops.admin.base.exception.AdminBizException;
import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.base.validator.InputParamsValidator;
import com.coocaa.ops.common.core.base.util.ResourceStatusValidator;
import com.coocaa.ops.common.core.biz.tab.entity.TabInfoEntity;
import com.coocaa.ops.common.core.biz.tab.entity.TabPanelConfigInfo;
import lombok.experimental.UtilityClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Objects;

/**
 * @author bijiahao
 * @description
 * @date 2020/6/18 17:18
 */
@Component
public class TabValidator {

    private static CommonLanguageProperties commonLanguageProperties;

    /**
     * 保存前的校验
     *
     * @param entity
     */
    public static void validBeforeSave(TabInfoEntity entity) {

        if (ResourceStatusValidator.isOnline(entity.getStatus())) {
            throw new AdminBizException(commonLanguageProperties.getNotAudit());
        }
        //先设置为没有,后面如果有，会进行重新设置
        entity.setHasDmp(0);
        Assert.hasText(entity.getTabType(), commonLanguageProperties.getTab() + commonLanguageProperties.getResourceType() + commonLanguageProperties.getNotNull());
        Assert.notNull(entity.getBusinessType(), "businessType" + commonLanguageProperties.getNotNull());
        Assert.hasText(entity.getTabName(), commonLanguageProperties.getTab() + commonLanguageProperties.getName() + commonLanguageProperties.getNotNull());
        /*if(TabTypeConstants.isNormal(entity.getTabType())){
            Assert.notEmpty(entity.getPanelList(), "关联板块信息不能为空");
            validRlsPanel(entity.getPanelList());
        }*/
    }

    @Autowired
    public void setCommonLanguageProperties(CommonLanguageProperties commonLanguageProperties) {
        TabValidator.commonLanguageProperties = commonLanguageProperties;
    }

    private static void validRlsPanel(List<TabPanelConfigInfo> tabPanelConfigInfoList) {
        for (TabPanelConfigInfo tabPanelConfigInfo : tabPanelConfigInfoList) {
            if (Objects.equals(1, tabPanelConfigInfo.getIsDmpPanel())) {
                List<TabPanelConfigInfo> dmpPanelList = tabPanelConfigInfo.getDmpPanelList();
                for (TabPanelConfigInfo dmpPanel : dmpPanelList) {
                    InputParamsValidator.validDmpContentAvailable(dmpPanel.getId(), dmpPanel.getDmpRegistryInfo(), "panelID");
                }
            } else {
                Assert.notNull(tabPanelConfigInfo.getId(), "panelID" + commonLanguageProperties.getNotNull());
            }
        }
    }

}
